// const base = 'http://ok.haoran168.top/api'
const base = 'https://oa.sdbaizhi.com/api'
export default function request(requ, data) {
	return new Promise((resolve, reject) => {
		uni.request({
			...requ,
			data,
			header: {
				token: uni.getStorageSync('token')
			},
			success(res) {
				resolve(res?.data)
			},
			fail(error) {
				uni.showToast({
					title: 'Api Error',
					duration: 2000,
					success() {
						uni.hideLoading()
					}
				})
				reject(error)
			}
		})
	})
}

uni.addInterceptor('request', {
	invoke(args) {
		// console.log(args, '拦截器!!!!')
		args.url = base + args.url
	},
	success(args) {
		if (args.data.code == -2) {
			uni.login({
				success(res) {
					uni.request({
						url: "/site/login",
						method: 'POST',
						data: {
							code: res.code
						},
						success(res1) {
							uni.setStorage({
								key: 'token',
								data: res1.data.data.token
							})
						}
					})
				}
			})
		}
		uni.hideLoading()

	},
	fail(err) {
		console.log('interceptor-fail', err)
	},
	complete(res) {
		// console.log('interceptor-complete', res)
	}
})
